package cn.tedu.egmvc4.controller;

import cn.tedu.egmvc4.mapper.CustomersMapper;
import cn.tedu.egmvc4.pojo.entity.Customers;
import cn.tedu.egmvc4.pojo.entity.UserOrders;
import cn.tedu.egmvc4.pojo.vo.CustOrdersVO;
import cn.tedu.egmvc4.pojo.vo.CustomersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerMapping;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/customers/")
public class CustomersController {
    @Autowired(required = false)
    private CustomersMapper customersMapper;

    /**
     * 第1题
     * 请求地址：/customers/insert
     * 请求方法：POST
     * 请求体数据：顾客id custId、顾客手机custTel、顾客名字custName、地址address
     * 返回响应：String "添加成功"
     */
    @RequestMapping("insert")
    public String insert(Customers customers){
        customersMapper.insertCust(customers);
        return "添加成功";
    }

    /**
     * 第2题
     * 请求地址：/customers/selectById
     * 请求方法：GET
     * 请求体数据：顾客id, 姓名, 所有订单的信息List<UserOrders>
     * 返回响应：CustOrdersVO
     */
    @RequestMapping("selectById")
    public CustOrdersVO selectById(int custId){
        CustOrdersVO custOrdersVO = customersMapper.selectCustOrders(custId);
        return custOrdersVO;
    }

    /**
     * 第3题
     * 请求地址：/customers/selectByCity
     * 请求方法：GET
     * 请求体数据：姓名和地址
     * 返回响应：List<CustomersVO>
     */
    @RequestMapping("selectByCity")
    public List<CustomersVO> selectByCity(String city){
        List<CustomersVO> list = customersMapper.custSelectByCity(city);
        return list;
    }

    /**
     * 第4题
     * 请求地址：/customers/update
     * 请求方法：POSt
     * 请求体数据：Customers
     * 返回响应：更改成功
     */
    @RequestMapping("update")
    public String update(Customers customers){
        customersMapper.updateCust(customers);
        return "更改成功";
    }

    /**
     * 第5题
     * 请求地址：/customers/delete
     * 请求方法：GET
     * 请求体数据：List<String>
     * 返回响应：删除成功
     */
    @RequestMapping("delete")
    public String delete(String custId){
        List<String> list = new ArrayList<>();
        list.add(custId);
        customersMapper.deleteCust(list);
        return "删除成功";
    }
}
